/**
 * Progetto PRAISED.
 * 07/feb/2011
 */
package it.uniroma3.engine.util;

import it.uniroma3.core.model.Token;
import it.uniroma3.core.model.Tokenizer;

import java.util.List;
import java.util.Map;

/**
 * RomanNumbersFormatter : This class is responsible for managing spelled out the words with Roman numbers
 * 
 * @author Accosta Augusto
 * @author Lopez Elisabetta
 *
 */
public class RomanNumbersFormatter {
	
	private Map<String, String> romans2numbers;

	/**
	 * no-args constructor
	 */
	public RomanNumbersFormatter() {
//		try {
//			this.romans2numbers = ProteinUtils.loadAllProperty("RomansEncodingSpelledOut.properties");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		this.romans2numbers = Utils.spelledOutRomanNumbers;
	}
	
	/**
	 * This method substitutes Roman numbers that occur in the last token of text input with arabic numbers
	 * 
	 * @param text
	 * @return the new text
	 */
	public String substituteRomansNumbersLastTokenByArabicNumbers(String text) {
		StringBuffer strb = new StringBuffer();
		
		if(text != null) {
			List<String> tokenList = Tokenizer.getStringTokensByCharSetMatcher(text);
			
			if(tokenList.size() != 0 ) {
				String result = formatRomans(tokenList.get(tokenList.size() - 1)); 
			
				for (int i = 0; i < tokenList.size() - 1; i++) {
					strb.append(tokenList.get(i) + " ");
			}
				strb.append(result);
			}
		}
		
		return strb.toString();
	}
	
	/**
	 * This method substitutes Roman numbers that occur in the text input with arabic numbers
	 * 
	 * @param text
	 * @return the new text
	 */
	public String substituteRomansNumbersByArabicNumbers(String text) {
		StringBuffer strb = new StringBuffer();
		String result;
		
		if(text != null) {
			List<Token> tokenList = Tokenizer.getTokensByCharSetMatcher(text);
			
			for (Token token : tokenList) {
				result = formatRomans(token.getToken()); 
				token.setToken(result);
				
				strb.append(token.getToken() + " ");
			}
		}
		
		return strb.toString();
	}
	
	/**
	 * This method extracts the Roman numerals in the text input and replaces them with their numeric value
	 * 
	 * @param string
	 * @return the new text
	 */
	public String formatRomans(String text) {
		String result;
		
		if(this.romans2numbers.containsKey(text)) {
			result = this.romans2numbers.get(text);
		}
		else {
			result = text;
		}
		
		return result;
	}

}
